package com.shujia.wyh.day29.test.codingtest;


import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ServerDemo {
    public static void service(){
        //创建ServerSocket对象
        try {
            ServerSocket ss = new ServerSocket(12345);
            Socket s = ss.accept();

            while (true){
                String ip = s.getInetAddress().getHostAddress();
                Date date = new Date();
                SimpleDateFormat sdfg = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String s1 = sdfg.format(date);

                InputStream inputStream = s.getInputStream();
                byte[] bytes = new byte[1024];
                int length = inputStream.read(bytes);
                String string = new String(bytes, 0, length);
                System.out.println(ip+":"+s1);
                System.out.println(string);

                //给出反馈
                OutputStream outputStream = s.getOutputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                String line = br.readLine();
                outputStream.write(line.getBytes());
            }



        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public static void main(String[] args) {
        service();
    }
}
